/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itschapala.sistemas.septimo.Vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.Control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.Control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.Modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.Modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.Vista.enumeracion.TipoBusqueda;
import mx.edu.itschapala.sistemas.septimo.Vista.enumeracion.TipoVentana;

/**
 *
 * @author Sony
 */
public class ProductoListar extends javax.swing.JFrame {
    
    private final ProductoLnLocal productoControl;
    private DefaultTableModel modelo;
    private List<Producto> lista;
    private JFrame ventana;
    /**
     * Creates new form ProductoListar
     */
    public ProductoListar(ProductoLnLocal productoControl) {
        this.productoControl=productoControl;
        
        initComponents();
        String nombres[] = {"Codigo","Nombre","Precio","Departamento","Descripcion"};
        modelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEditable(int row, int column){
               return false; 
            }
        };
    jTable1.setModel(modelo);
    
    lista=productoControl.getTodos();
    for (Producto p : lista){
        
        String renglon []=new String[5];
        renglon[0]=p.getCodigo() +"";
        renglon[1]=p.getNombre();
        renglon[2]=p.getPrecio() + "";
        renglon[3]=p.getDepartamento();
        renglon[4]=p.getDescripcion();
        modelo.addRow(renglon);
        
        //jTable1.setModel(modelo);
      }
    }

   

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        Actualizar = new javax.swing.JButton();
        Registra = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(51, 153, 255));
        setForeground(new java.awt.Color(102, 153, 255));

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setForeground(new java.awt.Color(204, 204, 255));
        jPanel1.setLayout(new java.awt.GridLayout(1, 3, 1, 0));

        jButton1.setBackground(new java.awt.Color(51, 102, 255));
        jButton1.setForeground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Regresar Al menu");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        Actualizar.setBackground(new java.awt.Color(51, 102, 255));
        Actualizar.setForeground(new java.awt.Color(255, 255, 255));
        Actualizar.setText("Actualizar");
        Actualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ActualizarActionPerformed(evt);
            }
        });
        jPanel1.add(Actualizar);

        Registra.setBackground(new java.awt.Color(51, 102, 255));
        Registra.setForeground(new java.awt.Color(255, 255, 255));
        Registra.setText("Registrar");
        Registra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegistraActionPerformed(evt);
            }
        });
        jPanel1.add(Registra);

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));
        jPanel2.setForeground(new java.awt.Color(102, 153, 255));

        jScrollPane1.setBackground(new java.awt.Color(102, 153, 255));
        jScrollPane1.setForeground(new java.awt.Color(102, 102, 255));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jRadioButton1.setText("Contiene");

        jRadioButton2.setText("Inicia");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Sony\\Pictures\\Fotos proyecto\\ListaProducto.png")); // NOI18N
        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addGap(1, 1, 1)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //Iniciar
    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
       
   /////////////////////////////////////PRODUCTO////////////////////////////////////////////////////////     
        List<Producto> tmp= new ArrayList();
        if(jRadioButton1.isSelected()){
            tmp=productoControl.getPornombre(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda.CONTIENE);
        }else{
            tmp= productoControl.getPornombre(jTextField1.getText()+evt.getKeyChar() , TipoBusqueda.INICIA);
        }
        String nombres[]= {"Codigo", "Nombre","Precio","Departamento","Descripcion"};
        modelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEnable(int row, int column){
                return false;
            }
         };
        
        for (Producto p : tmp){
            String renglon []=new String [5];
            renglon[0]=p.getCodigo()+"";
            renglon[1]=p.getNombre();
            renglon[2]=p.getPrecio()+"";
            renglon[3]=p.getDepartamento();
            renglon[4]=p.getDescripcion();
            modelo.addRow(renglon);
        }
        jTable1.setModel(modelo);
        lista=tmp;
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void RegistraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegistraActionPerformed
       new ProductoCrearEditar(null, productoControl,TipoVentana.VENTANA_REGISTRO).setVisible(true);
       
    }//GEN-LAST:event_RegistraActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //MenuPrincipal a = new MenuPrincipal();
        //a.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void ActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ActualizarActionPerformed
        String nombres[]={"codigo","Nombre","Precio","Departamento","Descripcion"};
        modelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEditable(int row, int colum){
                return false;
            }};

            jTable1.setModel(modelo);
            lista=productoControl.getTodos();
            for(Producto p: lista){
                String renglon[]= new String[5];
                renglon[0]=p.getCodigo()+"";
                renglon[1]=p.getNombre();
                renglon[2]=p.getPrecio()+"";
                renglon[3]=p.getDepartamento()+"";
                renglon[4]=p.getDescripcion();
                modelo.addRow(renglon);
            }

    }//GEN-LAST:event_ActualizarActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount()==2){

            //System.out.println(jTable1.getSelectedRow());
            Producto p =lista.get(jTable1.getSelectedRow());

            if(ventana==null){
                ventana=new DialogoProducto(p, productoControl);
            }else{
                ventana.dispose();
                ventana=new DialogoProducto(p,productoControl);
            }
            ventana.setVisible(true);

        }

    }//GEN-LAST:event_jTable1MouseClicked

    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Actualizar;
    private javax.swing.JButton Registra;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
